package com.hpli.trigger.listener;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.hpli.domain.activity.service.IRaffleActivitySkuStockService;
import com.hpli.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: 汀
 * @CreateTime: 2025-04-14
 * @Description: 活动sku库存耗尽
 * @Version: 1.0
 */

@Slf4j
@Component
public class ActivitySkuStockZeroCustomer {

    @Value("{spring.rabbitmq.topic.activity-sku-stock-zero}")
    private String topic;

    @Resource
    private IRaffleActivitySkuStockService skuStock;

    @RabbitListener(queuesToDeclare = @Queue(value = "${spring.rabbitmq.topic.activity-sku-stock-zero}", durable = "true"))
    public void listener(String message) {
        try {
            log.info("监听活动sku库存消耗为0消息 topic:{} message:{}", topic, message);
            BaseEvent.EventMessage<Long> eventMessage = JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<Long>>() {
            }.getType());
            Long sku = eventMessage.getData();

            skuStock.clearActivitySkuStock(sku);
            skuStock.clearQueueValue();
        } catch (Exception e) {
            log.error("监听活动sku库存消耗为0消息失败 topic:{} message:{}", topic, message, e);
            throw new RuntimeException("监听活动sku库存消耗为0消息失败", e);
        }
    }
}
